package com.muqi.app.project.utils;

import android.app.Activity;

import com.muqi.app.project.contants.MContants;
import com.muqi.app.qlearn.student.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.ShareType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;

public class ShareDialog {
	private final UMSocialService mController = UMServiceFactory.getUMSocialService(MContants.UM_SHARE);
	private Activity mActivity;
	private String title;
	private String shareUrl;
	
	public ShareDialog(Activity mActivity, String title, String shareUrl){
		this.mActivity = mActivity;
		this.title = title;
		this.shareUrl = shareUrl;
		configPlatforms();
	}
	
	private void configPlatforms() {
        // 添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        // 添加腾讯微博SSO授权
       // mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
        // 添加QQ、QZone平台
        addQQQZonePlatform();
        // 添加微信、微信朋友圈平台
        addWXPlatform();
    }
	
	/**
     * @功能描述 : 添加QQ平台支持 QQ分享的内容， 包含四种类型， 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary,
     *       image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title :
     *       要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl
     *       : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 )
     * @return
     */
    private void addQQQZonePlatform() {
        // 添加QQ支持, 并且设置QQ分享内容的target url
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(mActivity, MContants.QQ_App_Id, MContants.QQ_App_Key);
        qqSsoHandler.setTitle(title);
        qqSsoHandler.setTargetUrl(shareUrl);
        qqSsoHandler.addToSocialSDK();
        // 添加QZone平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(mActivity,  MContants.QQ_App_Id, MContants.QQ_App_Key);
        qZoneSsoHandler.addToSocialSDK();
    }
    
    /**
     * @功能描述 : 添加微信平台分享
     * @return
     */
    private void addWXPlatform() {
        // 注意：在微信授权的时候，必须传递appSecret
        // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(mActivity, MContants.WX_APP_ID, MContants.WX_APP_KEY);
        wxHandler.setTitle(title);
        wxHandler.setTargetUrl(shareUrl);
        wxHandler.addToSocialSDK();
        // 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(mActivity, MContants.WX_APP_ID, MContants.WX_APP_KEY);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.setTitle(title);
        wxCircleHandler.setTargetUrl(shareUrl);
        wxCircleHandler.addToSocialSDK();
    }
    
    public void showShareDialog(){
    	 mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
    			 SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE );
    	 mController.setShareType(ShareType.NORMAL);
    	 mController.setShareContent(shareUrl);
    	 UMImage image = new UMImage(mActivity,R.drawable.um_share_logo);
    	 mController.setShareImage(image);
		 mController.openShare(mActivity, false);
    }


	public void setTitle(String title) {
		this.title = title;
	}
    
}
